<?php
/**
 * QueenService: manage queens list and relations
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Philippe Bajoit <philippe@bajoit.net>
 */

if (!defined('DOKU_INC')) die();

class queenFamily {
	protected $queenList = array();
	protected $queenMap = array();
	protected $file;

	/**
	 * Constructor based on a JSON file
	 */
	public function __construct($file) {
		//$abs_file = DOKU_INC.'data/pages/json/'.$file.'.txt';
		//if (file_exists($abs_file)) {
			$this->queenList = json_decode(rawWiki('json:'.$file), true);
			foreach ($this->queenList as $q) {
				$this->queenMap[$q['id']] = $q;
			}
			$this->file = $file;
		//}
	}

	/**
	 * Save JSON in specified file
	 */
	private function p_persist($name, $format) {
		$jsonnewfilecontent = json_encode($this->queenList);
		// TODO pass $format = $this->lang[$lang_key] from DokuWiki_Syntax_Plugin
		saveWikiText('json:'.$name, $jsonnewfilecontent, sprintf($format, 'json:'.$name, 'json:'.$name));
	}

	/**
	 * Save JSON in original file
	 */
	public function persist($format) {
		p_persist($this->file, $format);
	}

	/**
	 * Get the queen by id
	 */
	public function getQueen($id) {
		return $this->queenMap[$id];
	}

	/**
	 * The list of queens owned / in a hive
	 */
	public function getOwnedQueens() {
		$rc = array();
		foreach ($this->queenList as $q) {
			if ($q['hive'] != null) {
				$rc[] = $q;
			}
		}
		return $rc;
	}

	/**
	 * The mother of a queen if defined
	 */
	public function getAncestor($queen) {
		return $this->queenMap[$queen['mother-id']];
	}

	/**
	 * The children of a queen if any
	 */
	public function getChildren($queen) {
		$rc = array();
		foreach ($this->queenList as $q) {
			if ($q['mother-id'] == $queen['id']) {
				$rc[] = $q;
			}
		}
		return $rc;
	}

	/**
	 * The evaluation of a queen for a given year if any
	 */
	public function getEval($queen, $year) {
		$rc = $queen['eval'];
		if ($rc != null) {
			return $rc[$year];
		}
		return null;
	}

	private function isInHive($l) {
		if (isset($l['hive'])) {
			$isInHive = true;
		} else {
			$isInHive = false;
			for ($i = 0; $i <= count($l['children']) - 1; $i++) {
				$isInHive = ($isInHive || $this->isInHive($l['children'][$i]));
			}
		}
		return $isInHive;
	}
	private function currentTree($l) {
		$r = $l;
		for ($i = count($r['children']) - 1 ; $i >= 0; $i--) {
			if ($this->isInHive($r['children'][$i])) {
				$r['children'][$i] = $this->currentTree($r['children'][$i]);
			} else {
				array_splice($r['children'], $i, 1);
			}
		}
		return $r;
	}
	private function getQueenSubTree($q) {
		$leaf = array();
		if (isset($q['hive']) || isset($q['death'])) {
			$leaf['year']  = queenColor($q['birth']);
			$leaf['url']   = $q['url'];
			$leaf['birth'] = $q['birth'];
			$leaf['dead']  = $q['death'];
		}
		$leaf['name'] = $q['name'];
		$leaf['hive'] = $q['hive'];
		$children = array();

		foreach ($this->getChildren($q) as $c) {
			$children[$c['birth'].$c['id']] = $this->getQueenSubTree($c);
		}
		if (sizeof($children) > 0) {
			ksort($children);
			$leaf['children'] = array();
			foreach ($children as $key => $val) {
				$leaf['children'][] = $val;
			}
		}
		return $leaf;
	}
	public function getQueensTree($year) {
		$root = array();
		$root['year']  = '#000';
		$root['name']  = 'Rucher';
		$root['url']   = 'http://apiwiki.bajoit.net';
		$root['birth'] = '';
		$root['dead']  = '';
		$children = array();

		foreach ($this->queenList as $q) {
			if (!isset($q['mother-id'])) {
				$children[$q['id']] = $this->getQueenSubTree($q);
			}
		}
		ksort($children);
		$root['children'] = array();
		foreach ($children as $key => $val) {
			$root['children'][] = $val;
		}

		// filter
		if (isset($year)) {
			// TO DO
		} else {
			$root = $this->currentTree($root);
		}

		// rebalance
		//$root = $this->balanceTree($root);

		return $root;
	}
}

function queenColor($birthYear) {
	$mod = $birthYear % 5;
	switch($mod) {
		case 1: return '#eee';
		case 2: return '#ff0';
		case 3: return '#f00';
		case 4: return '#0f0';
		default: return '#00f';
	}
}

function queenColorBg($color) {
	if ((strcasecmp($color,'#fff') == 0)||(strcasecmp($color,'#ffffff') == 0)||
			(strcasecmp($color,'#ff0') == 0)||(strcasecmp($color,'#ffff00') == 0)) {
		return '#666';
	}
	return null;
}